require(['grid','validator','select2'], function () {
	var editDialog,funName, $search = $('#search'),$grid = $('#grid'),vrsnType="",$softwareUpgrade=$("#softwareUpgrade"),$toPage=$("#toPage"),logType="ULOG0001",
	type,lineId,carId,appUpgrade;

	var selectMap = {};//下拉列表数据集

	//顶部类型
	$(".element-select a").click(function(){
		vrsnType = $(this).attr('val');
		$(this).addClass("active");
		$(this).siblings().removeClass("active");
	});

	$("a[val='APUP0001']",".element-select").click();

	//grid初始化
	function gridInit() {
		$grid.grid({
			url: "/upgrade/searchUpgrade.htm",
			colModel: [
				{name: "vrsnId", label: "vrsnId",hidden: true},
				{name: "appupgradeid", label: "appupgradeid", hidden: true},
				{name: "eqmtCd", label: "logId", hidden: true},
				{name: "eqmtId", label: "eqmtId", hidden: true},
				{name: "carModel", label: "carModel", hidden: true},
				{name: "eqmtType", label: "eqmtType", hidden: true},
				{name: "carId", label: "carId", hidden: true},
				{name: "lineId", label: "lineId", hidden: true},
				{name: "logId", label: "logId",hidden: true},
				{name: "licPltNo", label: "车牌号"},
				{name: "carInCd", label: "车辆自编号"},
				{name: "logType", label: "logType", hidden: true},
				{name: "vrsnName", label: "当前版本名称"},
				{name: "updateVerson", label: "升级版本名称"},
				{name: "upgradeSt", label: "升级状态",dict:'UPST0000'},
				{name: "stMsg", label: "升级消息",dict:'UPSM0000'},
				{name: "operate", width: 80, label: '日志信息', title: false,align:'center',
					formatter: 'operArea',
					formatoptions: [{
						text: '详细',
						css:'operate-edit',
						click: function (data) {
							if(data.logId){
								$.messager.confirm('跳转', '确定要进入升级日志吗？', 'toPage', function (r) {
									if (r) {
										var params = {};
										params.carIds = data.carId;
										params.logId = data.logId;
										params.logType = data.logType;
										var logId = data.logId;
										// 跳转到升级日志
										$.openTab('781ce7c7d2794ca0b8ab2a4473bd8109', 'gotoUpgradeDetail', [params]);
									}
								});
							}else{
								$.messager.alert('提示', '该数据无升级日志')
							}
						}
					}]
				},
			],
			height:$(window).height()-180,
			gridComplete:function(){
				var rowdata = $grid.getRowData();
				for(var i=0;i<rowdata.length;i++){
					var car = rowdata[i].carId;
					var eqmtType = rowdata[i].eqmtType;
					var carModel = rowdata[i].carModel;
					setJqgridVrsnName(i+1,rowdata.length,function(index,selectStr){
						$grid.setCell(index,"updateVerson",selectStr);
						$('tr:eq(' + index + ') select', $grid).select2();
					},car,eqmtType,carModel);
				}
				rowids =[];
			}
		});
	}

	//jqgrid中的对应版本
	function setJqgridVrsnName(index,length,callback,car,eqmtType,carModel){
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		var postDate = {};
		postDate.vrsnType = vrsnType;
		postDate.orgIds = car;

		if(vrsnType=="VERS0002"){
			orgIds = commonFun.getLeftTreeCheckedNodes("LINE");
			postDate.vrsnType = vrsnType;
			postDate.orgIds = orgIds;
			if(!orgIds){
				$.messager.alert("提示", "请选择线路查询！", "info");
				return;
			}
		}else if(vrsnType =="APUP0001"){
			postDate.appUpgrade = vrsnType;
			postDate.vrsnType = "";
			postDate.eqmtType = eqmtType||'0';
		}else if(vrsnType =="APUP0002"){
			postDate.appUpgrade = vrsnType;
			postDate.vrsnType = "";
            postDate.carModel = carModel||'0';
		}else if(vrsnType=="VERS0012"){
			postDate.carModel = carModel||'0';
		}
		$.ajaxExt({
			url : "/upgrade/getVersionName.htm",
			data: postDate
		}).done(function(result){
			var vrsnName="";
			var sel="<select id='jqgridUpgradeName'  name='jqgridUpgradeName' style='width: 180px;' class='u-input'>";
			$(result).each(function(i,item) {
				sel+='<option value='+item.vrsnId+'>'+item.vrsnName+'</option>';
				//加入到下拉列表数据集
				selectMap[item.vrsnId]=item.vrsnName;
			});
			sel+=" </select>";
			if(typeof callback === 'function'){
				callback(index,sel);
			}
			if(index==length){
				var sel = "<option value=''>批量更改请选择</option>";
				for(var prop in selectMap){
					if(selectMap.hasOwnProperty(prop)){
						sel += '<option value=' + prop + '>' + selectMap[prop] + '</option>';
					}
				}
				$("#vrsnName").html(sel);
			}
		});
	}

	// 跳转到详细页面
	function toUpgradeFileSetPage(){
		$.openTab('4f12e8e4937740908a2a73ae70b8fbd6', '');
	}

	//查询grid
	$search.on("click", function () {
		var postData ={};
		var type = commonFun.getLeftTreeCurrentTreeType();
		var orgIds = commonFun.getLeftTreeCheckedNodes(type);
		if (!orgIds) {
			$.messager.alert("提示", "请选择查询车辆/机构/驾驶员！", "info");
		} else {
			if(!vrsnType){
				$.messager.alert("提示", "请选择版本类型！", "info");
				return;
			}else{
				if(vrsnType=="VERS0002"){
					logType="ULOG0002";
				}else if(vrsnType =="APUP0001"){
					logType="ULOG0001";
				}else if(vrsnType =="APUP0002"){
					logType="ULOG0007";
				}else if(vrsnType =="VERS0012"){
					logType="ULOG0018";
				}
				//清空下拉列表数据集
				selectMap={};
				postData.type = type;
				postData.orgIds = orgIds.toString();
				postData.vrsnId = $("#vrsnName").val();
				postData.vrsnType = vrsnType;
				postData.logType = logType;
				$grid.grid('setGridParam', {
					datatype: "JSON",
					page: 1,
					postData:  postData
				}).trigger("reloadGrid");
			}
		}
	});

	$('#vrsnName').change(function() {
		var sel = $("#vrsnName").val()||'0';
        if('0'!=sel){
            $("select[name='jqgridUpgradeName']").select2('val',sel)
        }
	})

	//升级
	$("#softwareUpgrade").click(function(){
		var row= $grid.getGridParam("reccount")
		var rowIds = $grid.jqGrid("getGridParam", "selarrrow");
		if (rowIds.length < 1) {
			$.messager.alert("提示","请至少选择一个设备");
			return false;
		}
		var eqmtIdArr=[];
		var eqmtCdArr=[];
		var appupgradeidArr=[];
		var updateVersonArr=[];
		var carIdArr=[];
		var vrsnNameArr=[];
		var temp;
		var vrsnId;
		//升级类型
		var upgType=$grid.getRowData(rowIds[0]).appupgradeid;
		for(var i = 0; i < rowIds.length; i ++){
			var rowData =$grid.getRowData(rowIds[i]);
			tempId=$("#grid select:eq("+(rowIds[i]-1)+")").val();
			tempName=$grid.find("option:selected:eq("+(rowIds[i]-1)+")").text()
			vrsnId=$grid.find("option:selected:eq("+(rowIds[i]-1)+")").val()
			if(tempId){
				eqmtIdArr.push(rowData.eqmtId);
				eqmtCdArr.push(rowData.eqmtCd);
				appupgradeidArr.push(vrsnId);
				updateVersonArr.push(tempName);
				carIdArr.push(rowData.carId);
				vrsnNameArr.push(rowData.vrsnName);
			}else{
				$.messager.alert("警告","升级版本名称不能为空,请对应好相应的车辆跟升级版本!","warning");
				return false;
			}
		}
		$.ajaxExt({
			url : "/upgrade/softwareUpgrade.htm",
			data : {"eqmtIdArr":eqmtIdArr,"appupgradeidArr":appupgradeidArr,"updateVersonArr":updateVersonArr,"carIdArr":carIdArr,"eqmtCdArr":eqmtCdArr,"vrsnNameArr":vrsnNameArr,"appUpgrade":upgType}
		}).done(function(result){
			if(upgType=="VERS0002"){
				appUpgrade="2";
			}else if(upgType =="APUP0001"){
				appUpgrade="1";
			}
			else if(upgType =="APUP0002") {
				appUpgrade = "3";
			}
			else if(upgType =="VERS0012"){
					appUpgrade="12";
				}
			var data = {};
			var type = commonFun.getLeftTreeCurrentTreeType();
			var orgIds = commonFun.getLeftTreeCheckedNodes(type);
			data.type=type;
			data.appUpgrade = appUpgrade;
			data.carIds = orgIds;
			data.orgIds = orgIds;
			console.info(data)
			$.openTab('781ce7c7d2794ca0b8ab2a4473bd8109', 'gotoSearch', [data]);
		});
	});

	//跳转
	$toPage.on("click", function () {
		$.openTab('4f12e8e4937740908a2a73ae70b8fbd6','');
	});

	gridInit();
});